package com.ly.hotel.spainterface.pp.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.ly.hotel.R;
import com.ly.hotel.spainterface.pp.bean.PreItemBean;
import com.ly.hotel.spainterface.pp.bean.PreJSDataBean;

import java.util.List;

/**
 * Created by mo on 2018/6/5.
 */

public class PpJSSearchDialog extends Dialog implements View.OnClickListener {

    private Context context;
    private StringBuilder numStringBuilder;
    private ImageButton searchDialogClose;
    private EditText numEditText;
    private Button buttonOne;
    private Button buttonTwo;
    private Button buttonThree;
    private Button buttonFour;
    private Button buttonFive;
    private Button buttonSix;
    private Button buttonSeven;
    private Button buttonEight;
    private Button buttonNine;
    private Button buttonZero;
    private Button buttonEmpty;
    private Button buttonSearch;
    private List<PreJSDataBean> preJSDataBeans;
    private List<PreItemBean> itemBeans;
    private boolean isShowCall;
    private String artNo;

    public PpJSSearchDialog(Context context, int themeResId) {
        super(context, themeResId);
        this.context = context;
        numStringBuilder = new StringBuilder();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pp_search_technician_layout);
        initView();
        setSearchDialogClose();
    }

    private void initView() {
        searchDialogClose = findViewById(R.id.search_dialog_close);
        numEditText = findViewById(R.id.num_edit_text);
        buttonOne = findViewById(R.id.button_one);
        buttonTwo = findViewById(R.id.button_two);
        buttonThree = findViewById(R.id.button_three);
        buttonFour = findViewById(R.id.button_four);
        buttonFive = findViewById(R.id.button_five);
        buttonSix = findViewById(R.id.button_six);
        buttonSeven = findViewById(R.id.button_seven);
        buttonEight = findViewById(R.id.button_eight);
        buttonNine = findViewById(R.id.button_nine);
        buttonZero = findViewById(R.id.button_zero);
        buttonEmpty = findViewById(R.id.button_empty);
        buttonSearch = findViewById(R.id.button_search);
        buttonOne.setOnClickListener(this);
        buttonTwo.setOnClickListener(this);
        buttonThree.setOnClickListener(this);
        buttonFour.setOnClickListener(this);
        buttonFive.setOnClickListener(this);
        buttonSix.setOnClickListener(this);
        buttonSeven.setOnClickListener(this);
        buttonEight.setOnClickListener(this);
        buttonNine.setOnClickListener(this);
        buttonZero.setOnClickListener(this);
        buttonEmpty.setOnClickListener(this);
        buttonSearch.setOnClickListener(this);
    }

    private void setSearchDialogClose() {
        searchDialogClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button_one:
                numStringBuilder.append(buttonOne.getText().toString());
                numEditText.setText(numStringBuilder.toString());
                break;

            case R.id.button_two:
                numStringBuilder.append(buttonTwo.getText().toString());
                numEditText.setText(numStringBuilder.toString());
                break;

            case R.id.button_three:
                numStringBuilder.append(buttonThree.getText().toString());
                numEditText.setText(numStringBuilder.toString());
                break;

            case R.id.button_four:
                numStringBuilder.append(buttonFour.getText().toString());
                numEditText.setText(numStringBuilder.toString());
                break;

            case R.id.button_five:
                numStringBuilder.append(buttonFive.getText().toString());
                numEditText.setText(numStringBuilder.toString());
                break;

            case R.id.button_six:
                numStringBuilder.append(buttonSix.getText().toString());
                numEditText.setText(numStringBuilder.toString());
                break;

            case R.id.button_seven:
                numStringBuilder.append(buttonSeven.getText().toString());
                numEditText.setText(numStringBuilder.toString());
                break;

            case R.id.button_eight:
                numStringBuilder.append(buttonEight.getText().toString());
                numEditText.setText(numStringBuilder.toString());
                break;

            case R.id.button_nine:
                numStringBuilder.append(buttonNine.getText().toString());
                numEditText.setText(numStringBuilder.toString());
                break;

            case R.id.button_zero:
                numStringBuilder.append(buttonZero.getText().toString());
                numEditText.setText(numStringBuilder.toString());
                break;

            case R.id.button_empty:
                numStringBuilder.setLength(0);
                numEditText.setText(numStringBuilder.toString());
                break;

            case R.id.button_search:
                artNo = numStringBuilder.toString();
                if (TextUtils.isEmpty(artNo)) {
                    CommentCustomToast.makeText(context, context.getResources().getString(R.string.input_art_no),
                            Toast.LENGTH_SHORT, false).show();
                } else {
                    if (preJSDataBeans != null) {
                        PreJSDataBean preJSDataBean = null;
                        boolean isContains = false;
                        for (int i = 0; i < preJSDataBeans.size(); i++) {
                            preJSDataBean = preJSDataBeans.get(i);
                            if (artNo.equals(preJSDataBean.getNo())) {
                                isContains = true;
                                break;
                            }
                        }
                        if (isContains) {
                            int cid = preJSDataBean.getCid();
                            String itemName = "";
                            if (itemBeans != null) {
                                for (int i = 0; i < itemBeans.size(); i++) {
                                    PreItemBean preJSItemBean = itemBeans.get(i);
                                    if (cid == preJSItemBean.getId()) {
                                        itemName = preJSItemBean.getName();
                                        break;
                                    }
                                }
                            }
                            PpJSDetailsDialog ppJSDetailsDialog = new PpJSDetailsDialog(context, R.style.introDialog);
                            ppJSDetailsDialog.setItemName(itemName);
                            ppJSDetailsDialog.setPreJSDataBean(preJSDataBean);
                            ppJSDetailsDialog.setShowCall(isShowCall);
                            ppJSDetailsDialog.setCanceledOnTouchOutside(false);
                            ppJSDetailsDialog.show();
                            dismiss();
                        } else {
                            CommentCustomToast.makeText(context, "请输入正确的技师号！",
                                    Toast.LENGTH_SHORT, false).show();
                        }
                    } else {
                        CommentCustomToast.makeText(context, "后台未添加技师数据！",
                                Toast.LENGTH_SHORT, false).show();
                    }
                }
                break;

            default:
                break;
        }
    }

    public void setPreJSDataBeans(List<PreJSDataBean> preJSDataBeans) {
        this.preJSDataBeans = preJSDataBeans;
    }

    public void setItemBeans(List<PreItemBean> itemBeans) {
        this.itemBeans = itemBeans;
    }

    public void setShowCall(boolean showCall) {
        isShowCall = showCall;
    }
}
